﻿using System;
using System.ComponentModel;
using System.Runtime.Serialization;
using Wpf.CommonTools.StringExtension;

namespace Convertor.Plugin.SendEmail
{
    [Serializable]
    public class SmtpClientViewModel : IEquatable<SmtpClientViewModel>, IEquatable<String>, INotifyPropertyChanged, ISerializable
    {
        private string id = Guid.NewGuid().ToString("N");

        public string Id
        {
            get { return id; }
            set { id = value; }
        }
        private int port = 25;

        public int Port
        {
            get { return port; }
            set { port = value; if (PropertyChanged != null) { PropertyChanged(this, new PropertyChangedEventArgs("Port")); } }
        }
        private string host;

        public string Host
        {
            get { return host; }
            set { host = value; if (PropertyChanged != null) { PropertyChanged(this, new PropertyChangedEventArgs("Host")); } }
        }
        private int timeout = 60;

        public int Timeout
        {
            get { return timeout; }
            set { timeout = value; if (PropertyChanged != null) { PropertyChanged(this, new PropertyChangedEventArgs("Timeout")); } }
        }
        private string loginName;

        public string LoginName
        {
            get { return useDefaultCredentials ? "anonymous" : loginName; }
            set
            {
                loginName = value; if (PropertyChanged != null)
                {
                    PropertyChanged(this, new PropertyChangedEventArgs("LoginName"));
                    PropertyChanged(this, new PropertyChangedEventArgs("Reply"));
                    PropertyChanged(this, new PropertyChangedEventArgs("DisplayName"));
                }
            }
        }
        private string loginPassword;

        public string LoginPassword
        {
            get { return useDefaultCredentials ? null : loginPassword; }
            set { loginPassword = value; if (PropertyChanged != null) { PropertyChanged(this, new PropertyChangedEventArgs("LoginPassword")); } }
        }
        private bool enableSsl = false;

        public bool EnableSsl
        {
            get { return enableSsl; }
            set
            {
                enableSsl = value; 
                if (PropertyChanged != null)
                {
                    PropertyChanged(this, new PropertyChangedEventArgs("EnableSsl"));
                    PropertyChanged(this, new PropertyChangedEventArgs("LoginName"));
                    PropertyChanged(this, new PropertyChangedEventArgs("LoginPassword"));
                }
            }
        }
        private bool useDefaultCredentials = false;

        public bool UseDefaultCredentials
        {
            get { return useDefaultCredentials; }
            set { useDefaultCredentials = value; if (PropertyChanged != null) { PropertyChanged(this, new PropertyChangedEventArgs("UseDefaultCredentials")); } }
        }
        private bool enable = true;

        public bool Enable
        {
            get { return enable; }
            set { enable = value; if (PropertyChanged != null) { PropertyChanged(this, new PropertyChangedEventArgs("Enable")); } }
        }
        private string reply;

        public string Reply
        {
            get { return reply.ToEmpty(loginName); }
            set { reply = value; if (PropertyChanged != null) { PropertyChanged(this, new PropertyChangedEventArgs("Reply")); } }
        }
        private long errorCount;

        public long ErrorCount
        {
            get { return errorCount; }
            set { errorCount = value; if (PropertyChanged != null) { PropertyChanged(this, new PropertyChangedEventArgs("ErrorCount")); } }
        }
        private long successCount;

        public long SuccessCount
        {
            get { return successCount; }
            set { successCount = value; if (PropertyChanged != null) { PropertyChanged(this, new PropertyChangedEventArgs("SuccessCount")); } }
        }
        private string displayName;

        public string DisplayName
        {
            get { return displayName.ToEmpty(loginName); }
            set { displayName = value; if (PropertyChanged != null) { PropertyChanged(this, new PropertyChangedEventArgs("DisplayName")); } }
        }

        public void GetTemplate()
        {
            if (PropertyChanged != null) { PropertyChanged(this, new PropertyChangedEventArgs("EnableSsl")); }
        }

        public SmtpClientViewModel()
        {
        }

        #region IEquatable<SmtpClientModelView> 成员

        public bool Equals(SmtpClientViewModel other)
        {
            return this.Id == other.Id;
        }

        #endregion

        #region IEquatable<string> 成员

        public bool Equals(string other)
        {
            return this.Id == other;
        }

        #endregion

        #region INotifyPropertyChanged 成员

        public event PropertyChangedEventHandler PropertyChanged;

        #endregion

        #region ISerializable 成员

        public void GetObjectData(SerializationInfo info, StreamingContext context)
        {
            info.AddValue("Enable", Enable, typeof(bool));
            info.AddValue("EnableSsl", EnableSsl, typeof(bool));
            info.AddValue("Host", Host, typeof(string));
            info.AddValue("Id", Id, typeof(string));
            info.AddValue("LoginName", LoginName, typeof(string));
            info.AddValue("LoginPassword", LoginPassword, typeof(string));
            info.AddValue("Port", Port, typeof(int));
            info.AddValue("Reply", Reply, typeof(string));
            info.AddValue("Timeout", Timeout, typeof(int));
            info.AddValue("UseDefaultCredentials", UseDefaultCredentials, typeof(bool));
            info.AddValue("DisplayName", DisplayName, typeof(string));
        }

        public SmtpClientViewModel(SerializationInfo info, StreamingContext context)
        {
            this.enable = (bool)info.GetValue("Enable", typeof(bool));
            this.enableSsl = (bool)info.GetValue("EnableSsl", typeof(bool));
            this.host = (string)info.GetValue("Host", typeof(string));
            this.id = (string)info.GetValue("Id", typeof(string));
            this.loginName = (string)info.GetValue("LoginName", typeof(string));
            this.loginPassword = (string)info.GetValue("LoginPassword", typeof(string));
            this.port = (int)info.GetValue("Port", typeof(int));
            this.reply = (string)info.GetValue("Reply", typeof(string));
            this.timeout = (int)info.GetValue("Timeout", typeof(int));
            this.useDefaultCredentials = (bool)info.GetValue("UseDefaultCredentials", typeof(bool));
            try
            {
                this.DisplayName = (string)info.GetValue("DisplayName", typeof(string));
            }
            catch { }
        }

        #endregion
    }
}
